<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row shadow="hover" v-show="showSearch" class="ml10">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item label="座位号" prop="seatName">
            <el-input placeholder="请输入座位号" v-model="state.queryForm.seatName" />
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="seatDialogRef.openDialog('add')" v-auth="'infusion_seat_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <right-toolbar
              v-model:showSearch="showSearch"
              :export="'infusion_seat_export'"
              @exportExcel="exportExcel"
              class="ml10"
              style="float: right; margin-right: 20px"
              @queryTable="getDataList"
          ></right-toolbar>
        </div>
      </el-row>
      <el-table
          :data="state.dataList"
          v-loading="state.loading"
          style="width: 100%"
          border
          :cell-style="tableStyle.cellStyle"
          :header-cell-style="tableStyle.headerCellStyle">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="seatName" label="座位号" show-overflow-tooltip />
        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
        <el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip />
        <el-table-column :label="$t('common.action')" width="250">
          <template #default="scope">
            <el-button text type="primary" icon="edit-pen" v-auth="'infusion_seat_edit'" @click="seatDialogRef.openDialog('edit',scope.row.seatId)">{{
                $t('common.editBtn')
              }}</el-button>
            <el-tooltip :content="$t('sysrole.deleteDisabledTip')" placement="top">
							<span style="margin-left: 12px">
								<el-button
                    text
                    type="primary"
                    icon="delete"
                    v-auth="'infusion_seat_del'"
                    @click="handleDelete(scope.row.seatId)"
                >{{ $t('common.delBtn') }}
								</el-button>
							</span>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 角色编辑、新增  -->
    <seat-dialog ref="seatDialogRef" @refresh="getDataList()" />
  </div>
</template>

<script setup lang="ts" name="infusionConfig">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { pageSeatList, delSeatObj } from '/@/api/infusion/config';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useI18n } from 'vue-i18n';

// 引入组件
const SeatDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n();

// 定义变量内容
const seatDialogRef = ref();
const queryRef = ref();
const showSearch = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    seatName: '',
  },
  pageList: pageSeatList, // H
  descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
  queryRef.value.resetFields();
  getDataList();
};

// 导出excel
const exportExcel = () => {
  downBlobFile('/admin/infusion/seat/export', state.queryForm, 'seat.xlsx');
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    await delSeatObj(ids);
    getDataList();
    useMessage().success(t('common.delSuccessText'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
